#include "number.h"

void real_bounds_free (RealBounds* bounds)
{
    if (bounds)
    {
        if (bounds->min)
        {
            real_free(bounds->min);
        }
        
        if (bounds->max)
        {
            real_free(bounds->max);
        }
        
        free(bounds);
        bounds = NULL;
    }
}

RealBounds* real_bounds_new ()
{
    RealBounds* bounds = malloc(sizeof(RealBounds));
    
    if (bounds == NULL)
    {
        return NULL;
    }
    
    memset(bounds, 0, sizeof(RealBounds));
    
    bounds->min = real_pos_inf();
    bounds->max = real_neg_inf();
    
    return bounds;
}

void real_bounds_update (RealBounds* bounds, Real* real, int closed)
{
    int min_cmp;
    int max_cmp;
    
    if (min_cmp = real_cmp(bounds->min, real) != -1)
    {
        real_free(bounds->min);
        
        bounds->min = real_dup(real);
        bounds->min_closed = (min_cmp) ? closed : bounds->min_closed || closed;
    }
    
    if (max_cmp = real_cmp(bounds->max, real) != 1)
    {
        real_free(bounds->max);
        
        bounds->max = real_dup(real);
        bounds->max_closed = (max_cmp) ? closed : bounds->max_closed || closed;
    }
    
    real_free(real);
}
